package com.pinyougou.shop.service;

import com.pinyougou.pojo.TbSeller;
import com.pinyougou.sellergoods.service.SellerService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

/**
 * Title :商家登陆认证类
 *
 * @Project PinYouGou
 * @Description TODD
 * @Author 哈哈怪
 * @Date 2018/9/13 15:05
 **/
public class UserDetailsServiceImpl implements UserDetailsService{

    /**
     * 常量 : 商家申请状态 : 1--申请成功
     */
    private static final String SHOP_STATUS="1";

    private SellerService sellerService;

    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("......");
        //构建角色列表
        List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
        grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_SELLER"));

        //得到商家对象
        TbSeller seller = sellerService.findOne(username);
        if(seller != null){
            if(seller.getStatus().equals(SHOP_STATUS)){
                return  new User(username,seller.getPassword(),grantedAuthorityList);
            }else{
                return null;
            }
        }else{
            return null;
        }

    }
}
